﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZX.OA.Model;
using System.Data.Entity;

namespace ZX.OA.DAL
{
    public class PowerRoleDAL
    {
        Entity entity = new Entity();
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="uname"></param>
        /// <param name="upass"></param>
        /// <returns></returns>
        public int Login(string uname, string upass)
        {
            List<UserInfo> list = entity.UserInfo.SqlQuery("select * from UserInfoes where UserName='" + uname + "'").ToList();
            if (!string.IsNullOrEmpty(list[0].UserName))
            {
                if (upass == list[0].UserPwd)
                {
                    return 1;//代表登录成功
                }
                else
                {
                    return 2;//代表密码错误
                }
            }
            else
            {
                return 0;//代表用户名不存在
            }
        }
        /// <summary>
        /// 根据角色编号获取权限
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public List<RolePowerInfo> GetPowerByRoleID(string uname, string upass)
        {
            List<UserInfo> list = entity.UserInfo.SqlQuery("select * from UserInfoes where UserName='" + uname + "' and UserPwd='" + upass + "'").ToList();
            return entity.RolePowerInfo.Where(rp => rp.RoleID == list[0].RoleId).ToList();
        }
        /// <summary>
        /// 根据权限编号获取权限
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public PowerInfo GetPowerByPid(int pid)
        {
            return entity.PowerInfo.Find(pid);
        }
        /// <summary>
        /// 显示角色信息
        /// </summary>
        /// <returns></returns>
        public List<RoleInfo> ShowRoleInfo()
        {
            return entity.RoleInfo.ToList();
        }
        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <param name="ProviceID"></param>
        /// <returns></returns>
        public List<PowerInfo> ShowPower(int ProviceID)
        {
            return entity.PowerInfo.Where(p => p.ProviceID == ProviceID).ToList();
        }
        /// <summary>
        /// 获取个人考勤表
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="UserPass"></param>
        /// <returns></returns>
        public List<AttendanceInfo>ShowAttendanceInfoByUserID(string UserName,string UserPass)
        {
            List<UserInfo> list = entity.UserInfo.SqlQuery("select * from UserInfoes where UserName='" + UserName + "' and UserPwd='" + UserPass + "'").ToList();
            int userid = list[0].UserId;
            return entity.AttendanceInfo.Where(a => a.UserId == userid).ToList();
        }
        /// <summary>
        /// 显示总考勤
        /// </summary>
        /// <returns></returns>
        public List<AttendanceInfo>ShowAttendance()
        {
            return entity.AttendanceInfo.ToList();
        }
    }
}
